package com.shop.service.impl;

import com.github.pagehelper.PageInfo;
import com.shop.entity.Review;
import com.shop.mapper.ReviewMapper;
import com.shop.service.ReviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("reviewService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class,
        propagation = Propagation.REQUIRED)
public class ReviewServiceImpl implements ReviewService {

    private final ReviewMapper reviewMapper;

    @Override
    public PageInfo<Review> listReview(Integer pageNum, Integer pageSize) {
        List<Review> listReview = reviewMapper.listReview(pageNum, pageSize);
        return new PageInfo<>(listReview);
    }

    @Override
    public List<Review> getReviewById(Integer id) {
        List<Review> listReview = reviewMapper.Review(id);
        return new ArrayList<>(listReview);
    }

    @Override
    public void addReview(Review review) {
        reviewMapper.addReview(review);
    }

    @Override
    public void deleteReview(Integer id) {
        reviewMapper.deleteReview(id);
    }

    @Override
    public void updateReview(Review review) {
        reviewMapper.updateReview(review.getId(), review.getRating(), review.getComment());
    }
    @Override
    public int getTotal() {
        return reviewMapper.getTotal();
    }
}
